<?php namespace Home\Controller;

use Hdphp\Controller\Controller;

class LoginController extends Controller{
	
	
	public function index(){
		if(IS_POST){
			//接收提交的用户名
			$account = Q('post.account');
			//接收提交的密码 并用md5加密
			$pwd = Q('post.password','','md5');
			//接收提交的验证码
			$postcode = Q('post.captcha');
			//把验证码转小写
			$postcode = strtolower($postcode);
			//把session的验证码也转为小写 作比对
			$sessioncode = strtolower($_SESSION['code']);
			if($postcode != $sessioncode){
				View::error('验证码错误');
				return;
			}
			//把提交的用户名和密码与数据库的用户表作比对 
			$check = Db::table('user')->where("account='{$account}' AND pwd='{$pwd}'")->get();
			if($check AND $sessioncode == $postcode){
				$_SESSION['account'] = $check[0]['account'];
				$_SESSION['cusid'] = $check[0]['cusid'];
				go(U('Index/index'));
			}else{
				View::error('用户名或密码错误');
			}
		}
		View::make();
	}
	
	//退出登录方法  
	public function logout(){
		//删除指定的session
		unset($_SESSION['account']);
		unset($_SESSION['cusid']);
		//再跳转页面回登录页面
		go(U('Index/index'));
	}
	
	//显示验证码方法
	public function code(){
		Code::make();
	}
	
} 



 ?>